<?
/**
*  签约客户管理
*  2013/7/14 by zh
*/
include("../webkit/include/simple_db.php");

include("session.php");
require("ajax_base.php");

$db = new SimpleDB();
$action = $_GET['action'];

if($action == "del") {
	$db->doDelete('hncc_client', 'id='.$_GET['id']); 
	echo "ok";

} else if($action == "list") {
	$condition = "1=1";
	if(isset($_GET['client_source'])) {
		$condition .= " and client_source=".$_GET['client_source'];
	}
	if(isset($_GET['service_type'])) {
		$condition .= " and service_type=".$_GET['service_type'];
	}
	if(isset($_GET['client_name'])) {
		$condition .= " and client_name like '%".$_GET['client_name']."%'";
	}
	if($_SESSION['user_flag'] == "user") {
		$condition .= " and user_name = '".$_SESSION['uid']."'";
	}
	$sql = "select a.id,client_name,service_type,order_price,order_count, order_count*order_price as order_sum, order_date, b.truename as user_name, client_source, money_rate, pay_period,client_area,client_type,client_status,service_desc from hncc_client a Left Join hncc_user b On a.user_name=b.username where $condition order by client_status desc, id desc"; 
	echo to_grid_json($db, $sql, $dic_data, array("client_name"=>"blue_color_mapper","user_name"=>"red_color_mapper", "money_rate"=>"money_rate_mapper"));

} else if($action == "get") {
	$sql = "select * from hncc_client where id=".$_GET['id']; 
	$arr = $db->getOneRecordArray($sql);
	echo array_to_json($arr);

} else if($action == "add") {
	unset($_POST['id']);
	$db->doInsert('hncc_client', $_POST);
	echo 'ok';

} else if($action == "edit") {
	$id = $_POST['id'];
	unset($_POST['id']);
	$db->doUpdate('hncc_client', $_POST, 'id='.$id);
	echo 'ok';

} else if($action == "for_setup") {
	unset($_POST['id']);
	$db->query("update hncc_client set service_desc=concat(service_desc, ';', '".$_POST['service_desc']."') where id=36");
	echo 'ok';

}


function money_rate_mapper($money_rate, $arr) {
	if($_SESSION['user_flag'] == "user" && $money_rate>0) { // 普通用户登录时，显示上要做减扣处理
 		if($arr['client_source'] == 1) { 
			$money_rate = $money_rate - 0.05;  // 结算时按5%减扣
		} else {
			$money_rate = $money_rate - 0.1;   // 结算时按10%减扣
		}
	}
	return $money_rate;
}

?>